﻿using Gp.Scripts.Core;
using Gp.Scripts.Equip;
using Helpers;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.GUI {
    public class UnitEquipSlotUIController : MonoBehaviour, IController {
        [SerializeField] private Transform weaponParent;
        [SerializeField] private EquipSlotMeleePart meleePartObj;
        [SerializeField] private EquipSlotRangedPart rangedPartObj;

        private BaseWeaponSlotPart _lastSlot;
        
        private EquipSlotItem[] _items; 
        

        protected void Start() {
            _items = new EquipSlotItem[weaponParent.childCount];
            for (int i = 0; i < weaponParent.childCount; i++) {
                _items[i] = weaponParent.GetChild(i).GetComponent<EquipSlotItem>();
                _items[i].Init(this, i);
                _items[i].gameObject.SetActive(false);
            }
            
            
            // this.RegisterEvent<Event_Unit.Select>(UpdateWeaponSlot)
            //     .UnRegisterWhenGameObjectDestroyed(gameObject);
            //
            // this.RegisterEvent<Event_Unit.Select>(_ => ClearUp())
            //     .UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void UpdateWeaponSlot(Event_Unit.Select args) {
            if (!args.Unit && !ObjectHelper.IsPlayer(args.Unit.gameObject)) return;

            weaponParent.gameObject.SetActive(true);

            var equipHolder = args.Unit.ComEquip;
            var weapons = equipHolder.Weapons;
            for (var i = 0; i < weapons.Length; i++) {
                if (i >= _items.Length) break;
                _items[i].UpdateSlot(weapons[i]);
                _items[i].gameObject.SetActive(true);
            }
            
            // 显示原来选中的武器
            OnSlotButtonDown(_items[equipHolder.CurWeaponIndex], args.Unit);
        }


        

        public void OnSlotButtonDown(EquipSlotItem item, BaseUnit unit) {
            foreach (var x in _items) {
                if (x == item) x.MarkSelect();
                else x.UnMarkSelect();
            }
            
            
            var holder = item.Entity.ComEquip;
            BaseWeaponSlotPart slot = null;


            // switch (item.Entity.Info) {
            //     case MeleeWeaponInfo meleeBaseWeaponType:
            //         meleePartObj.Show(meleeBaseWeaponType);
            //         slot = meleePartObj;
            //         break;
            //     case RangedWeaponInfo rangedBaseWeaponType:
            //         rangedPartObj.Show(rangedBaseWeaponType);
            //         slot = rangedPartObj;
            //         break;
            // }
            
            
            if (_lastSlot != null && slot != _lastSlot) {
                _lastSlot.Hide() ;
            }


            _lastSlot = slot;

            if (holder.CurWeaponIndex == item.Index) return;

            holder.ChangeCurWeapon(item.Index);
            Global.Get<BattleSystem>().ToLastState();
        }



        private void ClearUp() {
            if (_lastSlot != null) {
                _lastSlot.Hide();
            }
            weaponParent.gameObject.SetActive(false);
            _lastSlot = null;
        }

        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}